//
//  LicensedHomeVC.m
//  Doctors
//
//  Created by 王森 on 16/4/13.
//  Copyright © 2016年 王森. All rights reserved.
//

#import "LicensedHomeVC.h"
#import "UIButton+Block.h"
#import "MJRefresh.h"
#import "UIImageView+WebCache.h"
#import "InformationInputViewController.h"
#import "PlaceOrderViewController.h"
@interface LicensedHomeVC ()
{
    NSString *openAmtStr;
    NSString *orderAmtStr;
    NSString *avatarStr;
    NSString *nameStr;
    
    UILabel*  titleName;
    UIImageView *imageView;
    
}
@property(nonatomic, strong) MyLinearLayout *rootLayout;
@property(nonatomic, strong) NSDictionary *dataDic;

@end

@implementation LicensedHomeVC

- (void)viewDidLoad {
    [super viewDidLoad];
    [self setTextTitleViewWithFrame:CGRectMake(0, 0, 60, 20) title:@"执业医师--理大师" fontSize:18];
    [self makeUI];
    self.view.hidden=YES;
    
    [self setRequest];
    
    
    // Do any additional setup after loading the view from its nib.
}

-(void)makeUI
{
    
    
    UIScrollView *scrollView = [[UIScrollView alloc] initWithFrame:self.view.bounds];
    scrollView.mj_header=[MJRefreshNormalHeader headerWithRefreshingBlock:^{
        [self setRequest];
        [scrollView.mj_header endRefreshing];
        
    }];
    
    self.view = scrollView;
    
    scrollView.backgroundColor=UIColorFromRGB(0xf4f4f4);
    self.rootLayout = [MyLinearLayout linearLayoutWithOrientation:MyLayoutViewOrientation_Vert];
    self.rootLayout.myLeftMargin = self.rootLayout.myRightMargin = 0;
    self.rootLayout.gravity = MyMarginGravity_Horz_Fill;
    self.rootLayout.backgroundColor=UIColorFromRGB(0xf4f4f4);
    [scrollView addSubview:self.rootLayout];
    
    MyFloatLayout *itemLayout = [MyFloatLayout floatLayoutWithOrientation:MyLayoutViewOrientation_Vert];
    
    itemLayout.backgroundImage = [UIImage imageNamed:@"bg_jianbian"];
    
    itemLayout.heightDime.equalTo(@(SHIJI_HEIGHT*111));
    [self.rootLayout addSubview:itemLayout];
    
    imageView = [[UIImageView alloc] init];
    [imageView sd_setImageWithURL:[NSURL URLWithString:_dataDic[@"avatar"]] placeholderImage:nil];
    
    imageView.widthDime.equalTo(@(SHIJI_HEIGHT*51));
    imageView.heightDime.equalTo(@(SHIJI_HEIGHT*51));
    imageView.layer.cornerRadius=(SHIJI_HEIGHT*51)/2;
    imageView.layer.masksToBounds=YES;
    imageView.myTopMargin=SHIJI_HEIGHT*29;
    imageView.myLeftMargin=20;
    [itemLayout addSubview:imageView];
    
    
    
    titleName=[UILabel new];
    titleName.text=[NSString stringWithFormat:@"%@，欢迎回来",_dataDic[@"name"]];
    titleName.myLeftMargin=20;
    titleName.textColor=UIColorFromRGB(0xffffff);
    //    title.centerYPos.equalTo(itemLayout.centerYPos);
    
    //    v.myCenterYOffset = 10 <==> v.centerYPos.equalTo(@10)  表示视图v在布局视图的垂直居中并往下偏移10个点
    //    v.myCenterOffset = CGPointMake(10,10) <==> v.centerXPos.equalTo(@10);v.centerYPos.equalTo(@10)
    //
    titleName.heightDime.equalTo(@(SHIJI_HEIGHT*14));
    titleName.widthDime.equalTo(@(SHIJI_HEIGHT*200));
    //    title.myCenterYOffset = 0;
    //    title.centerYPos.equalTo(@10);
    titleName.myTopMargin=SHIJI_HEIGHT*47;
    [itemLayout addSubview:titleName];
    
    [self makeTableKaiXianMoneyLabel:@"本月开险金额（元）" andMoney:_dataDic[@"openAmt"] andButtonTitle:@"录入" andButtonTag:5 actionBlock:^(int tag){
        NSLog(@"%d",tag);
        
        ALLOC(InformationInputViewController, view);
        [view setReloadData:^{
            [self setRequest];
            
        }];
        
        [self pushNextViewController:view];
        
        
        
    }];
    
    
    [self makeTableKaiXianMoneyLabel:@"本月订单金额（元）" andMoney:_dataDic[@"orderAmt"] andButtonTitle:@"下单" andButtonTag:6 actionBlock:^(int tag){
        NSLog(@"%d",tag);
        
        ALLOC(PlaceOrderViewController, view);
        [self pushNextViewController:view];
        
    }];
    
    
    
}
-(void)setRequest{
    NSDictionary *dic=[NSDictionary dictionaryWithObjectsAndKeys:[self getArtisanId],@"artisanId", nil]
    ;
    
    [self addProgressHUD];
    
    [self requestApiWithUrl:@"/artisan/getMedicalInfo" dictionary:dic isCache:NO  success:^(id responseObject) {
        
        [self removeStatuslable];
        
        self.view.hidden=NO;
        
        _dataDic=responseObject[@"artisan"];
        
        titleName.text=[NSString stringWithFormat:@"%@，欢迎回来",_dataDic[@"name"]];
        
        UILabel *lable=[self.view viewWithTag:5];
        lable.text=_dataDic[@"openAmt"];
        UILabel *lable2=[self.view viewWithTag:6];
        lable2.text=_dataDic[@"orderAmt"];
        
        [imageView sd_setImageWithURL:[NSURL URLWithString:_dataDic[@"avatar"]] placeholderImage:nil];
        
        //        [self makeUI];
        
        NSLog(@"%@",responseObject);
        
    } faild:^(NSError *error) {
        
        [self showFailed:@"请检查您的网络"];
    }];
    
    
}

-(void)makeTableKaiXianMoneyLabel:(NSString *)string andMoney:(NSString *)moneyString andButtonTitle:(NSString *)title andButtonTag:(int )tag  actionBlock:(void(^)(int tag))action{
    
    
    
    MyFloatLayout *itemLayout2 = [MyFloatLayout floatLayoutWithOrientation:MyLayoutViewOrientation_Vert];
    
    itemLayout2.backgroundImage = [UIImage imageNamed:@"shouye－kaixianjine"];
    itemLayout2.myLeftMargin=itemLayout2.myRightMargin=16*SHIJI_WIDTH;
    itemLayout2.myTopMargin=SHIJI_HEIGHT*18;
    itemLayout2.heightDime.equalTo(@(SHIJI_HEIGHT*167));
    [self.rootLayout addSubview:itemLayout2];
    
    
    UILabel *kaiXianTitle=[UILabel new];
    
    kaiXianTitle.myLeftMargin=16*SHIJI_WIDTH;
    kaiXianTitle.myTopMargin=11*SHIJI_HEIGHT;
    kaiXianTitle.text=string;
    kaiXianTitle.heightDime.equalTo(@(SHIJI_HEIGHT*14));
    //    kaiXianMoney.widthDime.equalTo(@(SHIJI_HEIGHT*125));    kaiXianMoney.textColor=UIColorFromRGB(0x333333);
    kaiXianTitle.weight=1;
    
    [kaiXianTitle sizeToFit];
    
    [itemLayout2 addSubview:kaiXianTitle];
    
    UILabel *kaiXianMoneyLabel=[UILabel new];
    ;
    //    kaiXianMoneyLabel.backgroundColor=[UIColor redColor];
    kaiXianMoneyLabel.tag=tag;
    
    kaiXianMoneyLabel.textAlignment=NSTextAlignmentCenter;
    kaiXianMoneyLabel.myTopMargin=38*SHIJI_HEIGHT;
    kaiXianMoneyLabel.text=moneyString;
    kaiXianMoneyLabel.heightDime.equalTo(@(SHIJI_HEIGHT*30));
    kaiXianMoneyLabel.widthDime.equalTo(itemLayout2.widthDime);
    //    kaiXianMoneyLabel.flexedHeight = YES;
    kaiXianMoneyLabel.font=FONTSIZE(kaiXianMoneyLabel, 30);
    
    kaiXianMoneyLabel.textColor=UIColorFromRGB(0xfb5363);
    
    
    [itemLayout2 addSubview:kaiXianMoneyLabel];
    
    
    
    UIButton *luRuButton=[UIButton new];
    ;
    luRuButton.backgroundColor=UIColorFromRGB(0x00ccc2);
    luRuButton.tag=tag;
    
    luRuButton.myTopMargin=20*SHIJI_HEIGHT;
    luRuButton.heightDime.equalTo(@(SHIJI_HEIGHT*39));
    luRuButton.widthDime.equalTo(@(150*SHIJI_WIDTH));
    //    kaiXianMoneyLabel.flexedHeight = YES;
    
    luRuButton.myCenterXOffset = 0;
    luRuButton.myLeftMargin=95*SHIJI_WIDTH;
    BUTTON_SETTITLE(luRuButton, title);
    luRuButton.titleLabel.textColor=UIColorFromRGB(0xffffff);
    luRuButton.myRightMargin=20;
    luRuButton.layer.cornerRadius=19*SHIJI_HEIGHT;
    [itemLayout2 addSubview:luRuButton];
    
    
    [luRuButton addActionHandler:^(NSInteger sss) {
        action(tag);
        
    }];
    
    
    
    
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
 #pragma mark - Navigation
 
 // In a storyboard-based application, you will often want to do a little preparation before navigation
 - (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
 // Get the new view controller using [segue destinationViewController].
 // Pass the selected object to the new view controller.
 }
 */

@end
